﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.User
{
    public class Favorite
    {
        /// <summary>
        /// Return list of favorite
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.User.Favorite> GetListOfFavorite(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR004_ListUserFavorite", 0, objListOfParameters);
                
                List<LimpaGaragem.Model.User.Favorite> objListOfFavorite = new List<LimpaGaragem.Model.User.Favorite>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.User.Favorite objFavorite = new LimpaGaragem.Model.User.Favorite();
                    objFavorite.IDUser = IDUser;
                    objFavorite.ID = Convert.ToInt32(objDataRow["ID_FAVORITE"]);
                    objFavorite.IDUserFavorite = Convert.ToInt32(objDataRow["ID_USER_FAVORITE"]);
                    objFavorite.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfFavorite.Add(objFavorite);
                }
                return objListOfFavorite;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new favorite
        /// </summary>
        /// <returns>ID FAVORITE</returns>
        public int InsertFavorite(int IDFavoriteUser, int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER_FAVORITE", IDFavoriteUser));
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR004_InsertUserFavorite", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete favorite
        /// </summary>
        public void DeleteFavorite(int IDFavorite)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_FAVORITE", IDFavorite));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR004_DeleteUserFavorite", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
